﻿package Gameplay
{
	import Gameplay.Events.MarkFailedEvent;
	
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.media.SoundChannel;
	import flash.text.TextField;
		
	public class Track extends AbstractTrack
	{		
		protected var FAllGood:Boolean;
		protected var FGoodPoints:int;
		protected var FMouseStrips:Vector.<FlashStrip>;
		
		public function Track():void
		{
			super();
			
			FAllGood = false;
			FGoodPoints = 0;
			FGoodText.text = '';
			
			addEventListener(MarkFailedEvent.MARK_FAILED, OnMarkFail);			
			FMouseStrips = new Vector.<FlashStrip>;
			var vStrip:FlashStrip;
			for (var i in FStrips)
			{
				FStrips[i].addEventListener(MouseEvent.MOUSE_DOWN, OnStripMouseDown);
			}
		}
		
		protected function OnStripMouseDown(e:MouseEvent):void 
		{				
			FGame.PerformInfo(PerformKeyPress(FlashStrip(e.target).ID));
		}
		
		override public function Reset():void
		{
			super.Reset();
			
			FAllGood = false;
			FGoodPoints = 0;			
		}
		
		public function Play():SoundChannel
		{
			FChannel = FSound.play();			
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);			
			return FChannel;
		}
		
		private function OnEnterFrame(e:Event):void
		{
			FContainer.y = FChannel.position / FSound.length * FContainer.height - FContainer.height + FContainer.TrashHold;
		}
		
		private function OnMarkFail(e:MarkFailedEvent):void
		{
			FAllGood = false;
			FGoodPoints = 0;
			FGoodText.text = '';			
			FBadText.visible = true;
		}
		
		override protected function PerformKeyPress(ATrackID:int):BonusInfo
		{
			var vResult:BonusInfo = new BonusInfo();			
			vResult.Points = 0;
			vResult.ID = ATrackID;			
			if(FContainer.Check(ATrackID))
			{
				vResult.Points++;
				FStrips[ATrackID].Show(true);
				FAllGood = true;
				FGoodPoints++;
			}
			else
			{
				FStrips[ATrackID].Show(false);				
				OnMarkFail(null);								
				vResult.BonusType = vResult.BAD;
				FStatistic.Bad++;
			}
			
			if(FAllGood)
			{
				FBadText.visible = false;
				var vBonusPoints:int = 0;
				if(FGoodPoints > 1 && FGoodPoints < 6)
				{
					FGoodText.text = 'КОМБО ' + FGoodPoints;
					vResult.BonusPoints = FGoodPoints;
					vResult.BonusType = vResult.COMBO;
					FStatistic.Combo++;
				}
				else if(FGoodPoints == 6)
				{					
					vResult.Points += 100;
					vBonusPoints = FGoodPoints - 5;
					FGoodText.text = 'ХОРОШО ' + vBonusPoints;
					vResult.Points += 2 * vBonusPoints;
					vResult.BonusPoints = vBonusPoints;
					vResult.BonusType = vResult.GOOD;
					FStatistic.Good++;
				}
				else if(FGoodPoints > 6 && FGoodPoints < 11)
				{
					vBonusPoints = FGoodPoints - 5;
					FGoodText.text = 'ХОРОШО ' + vBonusPoints;
					vResult.Points += 2 * vBonusPoints;
					vResult.BonusPoints = vBonusPoints;
					vResult.BonusType = vResult.GOOD;
					FStatistic.Good++;					
				}
				else if(FGoodPoints >= 11)
				{						
					vBonusPoints = FGoodPoints - 10;
					FGoodText.text = 'ОТЛИЧНО ' + vBonusPoints;
					vResult.Points += 3 * vBonusPoints;
					vResult.BonusPoints = vBonusPoints;
					vResult.BonusType = vResult.PERFECT;
					FStatistic.Perfect++;
				}
			}
			
			FStatistic.Score += vResult.Points;
			return vResult;
		}
	}
}
